package com.fingard.dsp.bank.directbank.bocom13;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IStatement;
import com.fingard.dsp.bank.reqrespobj.statement.ReqQryStatement;
import com.fingard.dsp.bank.reqrespobj.statement.RetAtsStatement;
import com.fingard.dsp.bank.reqrespobj.statement.TransState;
import com.fingard.dsp.bank.reqrespobj.statement.detail.RetAtsStatementDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.StringReader;

/**
 * 交行资金清算平台(BOCOM13)对账单
 */
public class BOCOM13Statement extends BOCOM13Base implements IStatement {
    public RetAtsStatement d_Qry_Statement(ReqQryStatement p_reqQrySttm) throws Exception {
        RetAtsStatement retAts = new RetAtsStatement(p_reqQrySttm);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";
        int totalNum = 0;
        String field="\u000F";
        try {
            String transCode = "";
            if ("F".equals(p_reqQrySttm.statementType)) {
                transCode = "I42";
            } else if ("S".equals(p_reqQrySttm.statementType)) {
                transCode = "I41";
            } else {
                transCode = "I41,I42";
            }
            if (transCode.contains(",")) {
                boolean isCompleted = false;
                String[] tranCodeArray = transCode.split(",");
                for (String tranCode : tranCodeArray) {
                    String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", p_reqQrySttm.actNum);
                    if (StringHelper.isNullOrEmpty(corpNo)) {
                        retAts.writeRespCode("-1");
                        retAts.writeBatRespInfo("未找到账号[" + p_reqQrySttm.actNum + "]保险方ID映射,请检查BOCOM13/config.xml中'corpNoMapping'节点配置");
                        retAts.writeEnd();
                        return retAts;
                    }
                    String tmpNotifyDataPath = getNotifyDataDirectory(Format.Date8Format.format(p_reqQrySttm.getBusDate())) + File.separator + corpNo + "_" + tranCode + ".txt";
                    WriteBankLogLn("当前获取对账单结果文件本机路径:" + tmpNotifyDataPath);
                    if (new File(tmpNotifyDataPath).exists()) {
                        isCompleted = true;
                        String tmpRetStr = FileHelper.readFileText(tmpNotifyDataPath, getCharset());
                        WriteBankLogLn("当前获取对账单结果文件解压后结果数据:\r\n" + tmpRetStr);

                        StringReader strReader = new StringReader(tmpRetStr);
                        BufferedReader tmpReader = new BufferedReader(strReader);

                        //0.银行方 ID 1.银行分行代码 2.银行方业务日期 3.银行方交易日期 4.银行方交易时间 5.保险方 ID 6.总笔数 7.总金额
                        totalNum += Integer.parseInt(tmpReader.readLine().split(field)[6].trim());

                        while(true) {
                            String eachline = tmpReader.readLine();
                            if(eachline == null) {
                                break;
                            }
                            String[] detailNodes = eachline.split(field);
                            //记录序号 1.交易码2.银行分行代码3.银行方业务日投保单号期4.银行方交易日期5.银行方交易时间
                            //6.银行方流水号 7.保险方流水号8.投保单号9.保单号10.客户姓名11.证件类型12.证件号码
                            //13.公私标志 14.银行标志 15.收款行行号 16.账号 17.交易币种 18.交易金额19.收付种类
                            //20.代收协议编号
                            RetAtsStatementDetail tmpRetDetail = new RetAtsStatementDetail();
                            if (StringHelper.hasAnyChar(detailNodes[5])) {
                                tmpRetDetail.busTime = detailNodes[4]+detailNodes[5].trim();
                            }
                            //流水 应该填银行流水还是保险流水
                            if (StringHelper.hasAnyChar(detailNodes[7])) {
                                tmpRetDetail.askNumber = detailNodes[7].trim();
                            }
                            //银行方流水
                            if (StringHelper.hasAnyChar(detailNodes[6])) {
                                tmpRetDetail.bankOrderNo = detailNodes[6].trim();
                            }
                            //金额amount
                            if (StringHelper.hasAnyChar(detailNodes[18])) {
                                tmpRetDetail.amount = detailNodes[18].trim();
                            }
                            //借贷标志  01借付款 02贷收款
                            if ("I41".equals(transCode)) {
                                tmpRetDetail.cdFlag = "2";
                            }else{
                                tmpRetDetail.cdFlag = "1";
                            }

                            if (StringHelper.hasAnyChar(detailNodes[16])) {
                                tmpRetDetail.oppAct = detailNodes[16].trim();
                            }
                            if (StringHelper.hasAnyChar(detailNodes[10])) {
                                tmpRetDetail.oppActName = detailNodes[10].trim();
                            }

                            if (StringHelper.hasAnyChar(detailNodes[8])) {
                                tmpRetDetail.srcNote = detailNodes[8].trim();
                            }else if(StringHelper.hasAnyChar(detailNodes[9])){
                                tmpRetDetail.srcNote = detailNodes[9].trim();
                            }
                            if (StringHelper.hasAnyChar(detailNodes[19])) {
                                tmpRetDetail.busType = detailNodes[19].trim();
                            }
                            tmpRetDetail.transState = TransState.Success;
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "当前未收到交行的结果文件!";
                    }
                }
                if (isCompleted) {
                    tmpRespCode = "0";
                }
            } else {
                String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", p_reqQrySttm.actNum);
                if (StringHelper.isNullOrEmpty(corpNo)) {
                    retAts.writeRespCode("-1");
                    retAts.writeBatRespInfo("未找到账号[" + p_reqQrySttm.actNum + "]保险方ID映射,请检查BOCOM13/config.xml中'corpNoMapping'节点配置!");
                    retAts.writeEnd();
                    return retAts;
                }
                String tmpNotifyDataPath = getNotifyDataDirectory(Format.Date8Format.format(p_reqQrySttm.getBusDate())) + File.separator + corpNo + "_" + transCode + ".txt";
                WriteBankLogLn("当前获取对账单结果文件本机路径:" + tmpNotifyDataPath);

                if (new File(tmpNotifyDataPath).exists()) {
                    String tmpRetStr = FileHelper.readFileText(tmpNotifyDataPath, getCharset());
                    WriteBankLogLn("当前获取对账单结果文件解压后结果数据:\r\n" + tmpRetStr);

                    StringReader strReader = new StringReader(tmpRetStr);
                    BufferedReader tmpReader = new BufferedReader(strReader);

                    //0.银行方 ID 1.银行分行代码 2.银行方业务日期 3.银行方交易日期 4.银行方交易时间 5.保险方 ID 6.总笔数 7.总金额
                    totalNum = Integer.parseInt(tmpReader.readLine().split(field)[6].trim());

                    while(true) {
                        String eachline = tmpReader.readLine();
                        if(eachline == null) {
                            break;
                        }
                        String[] detailNodes = eachline.split(field);
                        //记录序号 1.交易码2.银行分行代码3.银行方业务日投保单号期4.银行方交易日期5.银行方交易时间
                        //6.银行方流水号 7.保险方流水号8.投保单号9.保单号10.客户姓名11.证件类型12.证件号码
                        //13.公私标志 14.银行标志 15.收款行行号 16.账号 17.交易币种 18.交易金额19.收付种类
                        //20.代收协议编号
                        RetAtsStatementDetail tmpRetDetail = new RetAtsStatementDetail();
                        if (StringHelper.hasAnyChar(detailNodes[5])) {
                            tmpRetDetail.busTime = detailNodes[4]+detailNodes[5].trim();
                        }
                        //流水 应该填银行流水还是保险流水
                        if (StringHelper.hasAnyChar(detailNodes[7])) {
                            tmpRetDetail.askNumber = detailNodes[7].trim();
                        }
                        //银行方流水
                        if (StringHelper.hasAnyChar(detailNodes[6])) {
                            tmpRetDetail.bankOrderNo = detailNodes[6].trim();
                        }
                        //金额amount
                        if (StringHelper.hasAnyChar(detailNodes[18])) {
                            tmpRetDetail.amount = detailNodes[18].trim();
                        }
                        //借贷标志  01借付款 02贷收款
                        if ("I41".equals(transCode)) {
                            tmpRetDetail.cdFlag = "2";
                        }else{
                            tmpRetDetail.cdFlag = "1";
                        }

                        if (StringHelper.hasAnyChar(detailNodes[16])) {
                            tmpRetDetail.oppAct = detailNodes[16].trim();
                        }
                        if (StringHelper.hasAnyChar(detailNodes[10])) {
                            tmpRetDetail.oppActName = detailNodes[10].trim();
                        }
                        if (StringHelper.hasAnyChar(detailNodes[8])) {
                            tmpRetDetail.srcNote = detailNodes[8].trim();
                        }else if(StringHelper.hasAnyChar(detailNodes[9])){
                            tmpRetDetail.srcNote = detailNodes[9].trim();
                        }
                        if (StringHelper.hasAnyChar(detailNodes[19])) {
                            tmpRetDetail.busType = detailNodes[19].trim();
                        }
                        tmpRetDetail.transState = TransState.Success;
                        retAts.writeRetDetail(tmpRetDetail);
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = "当前未收到交行的结果文件!";
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeBatRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }

        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("代收付对账单获取失败,原因:[" + tmpRespInfo + "]");
        } else {
            if (retAts.getWrittenRdCount() == 0) {
                retAts.writeRespCode("0");
                retAts.writeRespInfo("交易为空");
            } else {
                // 获取成功时写入总笔数
                retAts.isWritedTotalNum(String.valueOf(totalNum));
            }
        }

        retAts.writeEnd();
        return retAts;
    }
}